//Player Constructor
function Player(display,color,index) {
	this.Display = display;
	this.Color = color;
	this.Index = index;
	this.xPos = 0;
	this.yPos = 0;
	this.Section;
	this.Stats = new Object();
	this.resetStats();
	this.Attrb = new Object();
	this.Attrb["HP"] = new Array(2);
	this.Attrb["HP"][0]=0;
	this.Attrb["HP"][1]=0;
	this.Attrb["MP"] = new Array(2);
	this.Attrb["MP"][0]=0;
	this.Attrb["MP"][1]=0;
	this.Attrb["FC"]=0;
	this.Attrb["$"]=0;
	this.Skills = new Array();
	this.Time=0;
}

Player.prototype.statKey = function(num) {
	if (num==0) return "SPD";
	if (num==1) return "INT";
	if (num==2) return "WIL";
	if (num==3) return "SPI";
	if (num==4) return "DEX";
	if (num==5) return "AGI";
	if (num==6) return "PER";
	if (num==7) return "STR";
	if (num==8) return "TOU";
	if (num==9) return "CHA";
}

Player.prototype.resetStats = function() {
	var tot=0;
	for (var i in this.Stats) {
		tot += this.Stats[i];
	}

	this.Stats["AGI"]=1;
	this.Stats["CHA"]=1;
	this.Stats["DEX"]=1;
	this.Stats["INT"]=1;
	this.Stats["PER"]=1;
	this.Stats["SPD"]=1;
	this.Stats["SPI"]=1;
	this.Stats["STR"]=1;
	this.Stats["TOU"]=1;
	this.Stats["WIL"]=1;
	this.Stats["FRE"]=1;
	
	return (tot-10);
}

Player.prototype.move = function (y,x) {
	//Handle when the creature is on an even row.
	if (!(this.yPos%2) && y!=0) {x-=1;}
	
	var ny=this.yPos+y,nx=this.xPos+x;
	
	//Make sure that the play can move in that direction.
	if (ny < 0) return;
	if (ny > 23) return;
	if (nx < 0) return;
	if (nx > 79) return;
	if (!(this.Section.walkable(ny,nx))) return;
	
	//Move Player
	this.Section.Map[this.yPos][this.xPos][1]=-1;
	this.yPos+=y;
	this.xPos+=x;
	this.Section.Map[this.yPos][this.xPos][1]=this.Index;
	this.Section.World.Time += this.Section.World.BMVTime[this.Stats["SPD"]];
	this.Section.updateDisplay();
};

Player.prototype.onExit = function() {
	return (this.Section.exitDirection(this.yPos,this.xPos)!="");
}

Player.prototype.exitDirection = function() {
	return this.Section.exitDirection(this.yPos,this.xPos);
}

Player.prototype.moveSection = function() {
	var dir = this.exitDirection();
	var newsect = this.Section.World.nextSectionKey(this.Section.Key,dir);
	
	//Move Player
	this.Section.Map[this.yPos][this.xPos][1]=-1;
	this.Section = this.Section.World.Sections[newsect];
	dir = this.Section.World.reverseDirection(dir);
	this.yPos = this.Section.Exits[dir][0];
	this.xPos = this.Section.Exits[dir][1];
	this.Section.Map[this.yPos][this.xPos][1]=this.Index;
	this.Section.updateDisplay();
}

Player.prototype.updateStats = function() {
	var disp="";
	//Show Attrb
	disp+="HIT: "+this.Attrb["HP"][0]+"/"+this.Attrb["HP"][1]+"<br>";
	disp+="MAG: "+this.Attrb["MP"][0]+"/"+this.Attrb["MP"][1]+"<br>";
	disp+="BDF: "+"<br>";
	disp+="DOD: "+"<br>";
	disp+="FC : "+this.Attrb["FC"]+"<br>";
	disp+="$ &nbsp;: "+this.Attrb["$"]+"<br>";
	//disp+="INI"+"<br>";
	disp+="<br>";
	//Show Stats
	for (var key in this.Stats) {
		disp += key +": "
		disp += this.Stats[key] + "<br>";
	}
	disp += "<br>";
	this.Section.World.Stats.refresh(disp);
}